package com.itheima.controller;

/**
 * @author SWJ
 * @create 2021-08-12 22:14
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.log4j.helpers.QuietWriter;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * 体检检查项管理
 */
@RestController
//是直接让方法转化java对象转换jsion
@RequestMapping("/checkitem")
//  //res服务端返回结果
// axios.post("/checkitem/add.do", this.formData).then((res) => {
public class CheckItemController {
    @Resource
    //javax.annotation.Resource
    //作用相当于@Autowired，只不过@Autowired是byType自动注入，而@Resource默认byName自动注入。
    //区别：
    //@Reference注入的是分布式中的远程服务对象


    private CheckItemService checkItemService;
    //src/main/java/com/itheima/service/CheckItemService.java

    //新增检查项
    @RequestMapping("/add")
    //返回src/main/java/com/itheima/entity/Result.java

    //@RequestBod解析jsion提交数据
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
            //新增的语句
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
            //当检查项中是"新增检查项失败"
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        //则新增检查项成功
    }

    //8/18/增加检测项分页查询方法
    @RequestMapping("/findPage")
    //src/main/java/com/itheima/entity/QueryPageBean.java
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        /**@RestController 通过MVC把pageResult
        //是直接让方法转化java对象转换jsion*/
        return pageResult;
    }

    //8/19/删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            //创建deleteByid提供方法给service
            checkItemService.deleteByid(id);
        } catch (RuntimeException e) {
            //如果删除失败
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            //删除后进行提示信息
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //如果成功进行显示
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    ////编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        //@RequestBody传入的是jsion 对象CheckItem
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            //判断调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    //根据id当前数据的回显的方法
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            //根据id数据进行查询findById
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //加载检查项所有数据查询所有数据
    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItemList = checkItemService.findAll();
        if(checkItemList != null && checkItemList.size() > 0){
            //如果
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItemList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }



}

